package com.akaovi.chatroom.scene;

import com.akaovi.chatroom.process.ChatMessManage;
import javafx.application.Platform;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.control.Label;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundFill;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import org.kordamp.ikonli.javafx.FontIcon;

import java.util.Optional;

/**
 * @author akaovi
 * @version 1.0
 * @description  会话栏
 * @date 2022/12/9 16:15
 * @since 2022/12/9 16:15
 */
public class ChatList {
    public static VBox chatList = new VBox();  // 聊天栏
    static Font font = new Font(16);
    static Label chatText = new Label("消息列表");

    static {
        chatList.setSpacing(10);
        chatList.setAlignment(Pos.BASELINE_CENTER);
        chatList.setBackground(new Background(new BackgroundFill(new Color(0.98, 0.98, 0.98, 1),
                null, null)));
        chatText.setId("-1");
        chatText.setFont(new Font(18));
        chatText.setGraphic(new FontIcon("anto-team"));
        chatList.getChildren().add(chatText);
        Platform.runLater(() -> MainScene.gridPane.add(chatList, 1, 0, 2, 10));
    }

    /**
     * 添加一个在线好友会话
     * 没有校验是否已经有该用户，存在重复添加的风险
     *
     * 追溯源码，我们发现在其存储节点的空间初始化时，时对ArrayList的封装 super(new ArrayList<T>());
     * 不具有去重的功能
     * @param account  账号
     * @param username 用户名
     */
    public static void addFriendToChatList(int account, String username) {
        Platform.runLater(() -> {
            Label friend = new Label(username);
            friend.setId(String.valueOf(account));
            friend.setOnMouseClicked(e -> {
                Label beClicked = (Label) e.getSource();
                SendMessBoard.mainTitle.setText(beClicked.getText());
                SendMessBoard.currentId = beClicked.getId();
                ChatMessManage.removeAll();
                if (!SendMessBoard.mainTitle.isVisible()) {
                    SendMessBoard.showSendMessBoard();
                }
                //todo 调试
                System.out.println(SendMessBoard.currentId);
            });
            friend.setGraphic(new FontIcon("anto-user"));
            friend.setFont(font);
            chatList.getChildren().add(friend);
        });
    }

    /**
     * 展示会话栏
     */
    public static void showChatList() {
        if (SendMessBoard.currentId == null || SendMessBoard.currentId.length() == 0) {
            MainScene.showCommonIcon();
        } else {
            MainScene.coverCommonIcon();
        }
        chatList.setVisible(true);
        if (FriendsList.friendsList.isVisible()) {
            FriendsList.friendsList.setVisible(false);
        }
    }

    /**
     * 关闭会话栏
     */
    public static void coverChatList() {
        if (chatList.isVisible()) {
            chatList.setVisible(false);
        }
    }

    /**
     * 重置会话栏
     */
    public static void restart() {
        ChatList.chatList.getChildren().clear();
        ChatList.chatList.getChildren().add(chatText);
    }

    /**
     * 移除一个在线会话
     * 如果该用户在会话栏中
     *
     * @param account 用户账号
     */
    public static void removeOne(String account) {
        Platform.runLater(() -> {
            Optional<Node> first = chatList.getChildren().stream().filter(label -> label.getId().equals(account)).findFirst();
            first.ifPresent(node -> chatList.getChildren().remove(node));
        });
    }
}
